import { Body, Controller, Get, Post } from '@nestjs/common';
import { AnthropicService } from '../services/anthropic.service';
import { DeepSeekService } from '../services/deepseek.service';
import { MultimodalCompletionRequestDto } from '../dto/multimodal-message.dto';
import { ChatCompletionResponseDto } from '../dto/completion-response.dto';
import {
  ImageAnalysisRequestDto,
  ImageAnalysisResponseDto,
} from '../dto/image-analysis-request.dto';
import {
  JsonFormatterRequestDto,
  JsonFormatterResponseDto,
} from '../dto/json-formatter-request.dto';
import { ChatCompletionRequestDto } from '../dto/chat-completion-request.dto';
import { ApiTags, ApiOperation, ApiResponse, ApiBody } from '@nestjs/swagger';
// import {
//   IntegratedAnalysisRequestDto,
//   IntegratedAnalysisResponseDto,
// } from '../dto/integrated-analysis-request.dto';

@ApiTags('ai', 'anthropic')
@Controller('api/anthropic')
export class AnthropicController {
  constructor(
    private readonly anthropicService: AnthropicService,
    private readonly deepseekService: DeepSeekService,
  ) {}

  /**
   * 处理多模态消息请求
   *
   * @param request 多模态完成请求
   * @returns 生成的响应
   */
  @ApiOperation({
    summary: '生成多模态消息完成',
    description: '接收包含文本和图像的多模态消息，并生成AI回复',
  })
  @ApiBody({ type: MultimodalCompletionRequestDto })
  @ApiResponse({
    status: 200,
    description: '成功生成回复',
    type: ChatCompletionResponseDto,
  })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 500, description: 'AI服务调用失败' })
  @Post('messages')
  async generateMultimodalCompletion(
    @Body() request: MultimodalCompletionRequestDto,
  ): Promise<ChatCompletionResponseDto> {
    return this.anthropicService.generateMultimodalCompletion(request);
  }

  /**
   * 分析图片风格
   * @param request 图片分析请求
   * @returns 分析结果
   */
  @ApiOperation({
    summary: '分析图片风格',
    description:
      '分析图片（特别是小红书封面）的设计风格、文字排版和视觉元素特点',
  })
  @ApiBody({ type: ImageAnalysisRequestDto })
  @ApiResponse({
    status: 200,
    description: '图片分析成功',
    type: ImageAnalysisResponseDto,
  })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 500, description: 'AI服务调用失败' })
  @Post('analyze-image')
  async analyzeImage(
    @Body() request: ImageAnalysisRequestDto,
  ): Promise<ImageAnalysisResponseDto> {
    return this.anthropicService.analyzeImage(
      request.imageUrl,
      request.model,
      request.maxTokens,
    );
  }

  /**
   * 将文本内容转换为结构化的 JSON 格式
   * @param request JSON 格式化请求
   * @returns 格式化后的 JSON 数据
   */
  @ApiOperation({
    summary: '将文本转换为JSON格式',
    description: '将非结构化文本内容转换为结构化的JSON格式',
  })
  @ApiBody({ type: JsonFormatterRequestDto })
  @ApiResponse({
    status: 200,
    description: '格式化成功',
    type: JsonFormatterResponseDto,
  })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 500, description: 'AI服务调用失败' })
  @Post('format-json')
  async formatToJsonV2(
    @Body() request: JsonFormatterRequestDto,
  ): Promise<JsonFormatterResponseDto> {
    const res = await this.deepseekService.formatToJson(request.textContent);
    return res;
  }

  /**
   * 获取 DeepSeek 可用模型列表
   * @returns 模型列表信息
   */
  @ApiOperation({
    summary: '获取可用模型列表',
    description: '获取DeepSeek可用的AI模型列表',
  })
  @ApiResponse({
    status: 200,
    description: '成功获取模型列表',
    schema: {
      type: 'object',
      properties: {
        data: {
          type: 'array',
          items: {
            type: 'object',
            properties: {
              id: { type: 'string', example: 'deepseek-chat' },
              name: { type: 'string', example: 'DeepSeek Chat' },
            },
          },
        },
      },
    },
  })
  @ApiResponse({ status: 500, description: 'AI服务调用失败' })
  @Get('models')
  async getModels() {
    return this.deepseekService.getModels();
  }

  @ApiOperation({
    summary: '获取用户账户余额',
    description: '获取DeepSeek账户的剩余余额信息',
  })
  @ApiResponse({
    status: 200,
    description: '成功获取账户余额',
    schema: {
      type: 'object',
      properties: {
        balance: { type: 'number', example: 6250.5 },
        currency: { type: 'string', example: 'CNY' },
      },
    },
  })
  @ApiResponse({ status: 500, description: 'AI服务调用失败' })
  @Get('get-user-balance')
  async getUserBalance() {
    console.log('来了老弟');
    return this.deepseekService.getUserBalance();
  }

  /**
   * 生成聊天完成
   * @param request 聊天完成请求
   * @returns 聊天完成响应
   */
  @ApiOperation({
    summary: '生成聊天完成',
    description: '使用DeepSeek模型生成聊天回复',
  })
  @ApiBody({ type: ChatCompletionRequestDto })
  @ApiResponse({
    status: 200,
    description: '成功生成回复',
    type: ChatCompletionResponseDto,
  })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 500, description: 'AI服务调用失败' })
  @Post('chat-completions')
  async generateChatCompletion(
    @Body() request: ChatCompletionRequestDto,
  ): Promise<ChatCompletionResponseDto> {
    // 将ChatCompletionRequestDto转换为DeepSeekChatCompletionRequestDto
    const deepseekRequest = {
      ...request,
      model: request.model || 'deepseek-chat', // 确保model有值
    };
    return this.deepseekService.generateChatCompletion(deepseekRequest);
  }
}
